package com.example.projectdemo.rvrv;

import android.content.Context;
import android.os.Build;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.example.projectdemo.R;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

/***
 * 创建时间：2020/2/25 11:05
 * 创建人：10850
 * 功能描述：
 */
public class LeftAdapter extends RecyclerView.Adapter<LeftAdapter.LeftHolder> {

    private Context mContext;
    private List<String> mList;

    private int choosePos = 0;

    public LeftAdapter(Context context, List<String> list) {
        this.mList = list;
        this.mContext = context;
    }

    public void setChoose(int pos) {
        choosePos = pos;
        notifyDataSetChanged();
    }

    public interface OnItemClickListener {
        void onItemClick(View view, int position);

        void onItemLongClick(View view, int position);
    }

    private OnItemClickListener mOnItemClickListener;

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        mOnItemClickListener = onItemClickListener;
    }

    @NonNull
    @Override
    public LeftHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        View inflate = LayoutInflater.from(mContext).inflate(R.layout.left_item, viewGroup, false);
        return new LeftHolder(inflate);
    }

    @Override
    public void onBindViewHolder(@NonNull final LeftHolder leftHolder, int i) {
        leftHolder.mTextView.setText(mList.get(i));

        if (i == choosePos) {
            leftHolder.mTextView.setBackgroundResource(R.drawable.text_bg);
        } else {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
                leftHolder.mTextView.setBackgroundResource(R.drawable.text_bg_gray);
            }
        }

        if (mOnItemClickListener != null) {
            leftHolder.mTextView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    int pos = leftHolder.getLayoutPosition();
                    mOnItemClickListener.onItemClick(leftHolder.mTextView, pos);
                }
            });

            leftHolder.mTextView.setOnLongClickListener(new View.OnLongClickListener() {
                @Override
                public boolean onLongClick(View v) {
                    int pos = leftHolder.getLayoutPosition();
                    mOnItemClickListener.onItemLongClick(leftHolder.mTextView, pos);
                    return false;
                }
            });
        }
    }

    @Override
    public int getItemCount() {
        return mList == null ? 0 : mList.size();
    }

    public class LeftHolder extends RecyclerView.ViewHolder {

        private TextView mTextView;

        public LeftHolder(@NonNull View itemView) {
            super(itemView);
            mTextView = itemView.findViewById(R.id.left_tv);
        }
    }
}
